//  Android Carpool System
//  2011-2011
//  Author: Jinhuan Wang
//  name: Signin.java
//  description: provide method to sign in 
//$history: /carpool/scr/com/DataExange/Signin.java$
package com.carpool.message;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.carpool.UserInfo;

public class Signin {
	private static final String SERVLET_URL = "http://169.254.80.238:8080/carpooletservice/Signin";
	

	public static boolean signin(String name, String password, UserInfo uinfo) {
		boolean success;
		URL url = null;
		HttpURLConnection httpurlconnection = null;
		try {
			url = new URL(SERVLET_URL);
			httpurlconnection = (HttpURLConnection) url.openConnection();
			httpurlconnection.setRequestMethod("POST");
			httpurlconnection.setDoOutput(true);
			httpurlconnection.setDoInput(true);
			httpurlconnection.connect();
			DataOutputStream dos = new DataOutputStream(
					httpurlconnection.getOutputStream());
			dos.writeUTF(name);
			dos.writeUTF(password);
			dos.flush();
			dos.close();
			DataInputStream dis = new DataInputStream(
					httpurlconnection.getInputStream());
			int uid = dis.readInt();
			uinfo.setUserID(uid);
			String cellphone = dis.readUTF();
			uinfo.setCellphone(cellphone);
			String usertype=dis.readUTF();
			uinfo.setUserType(usertype);
			if (uid == 0 || cellphone == null) {
				success = false;

			} else {
				success = true;
				uinfo.save();
			}

		} catch (Exception e) {
			// e.printStackTrace();
			success = false;

		} finally {
			if (httpurlconnection != null)
				httpurlconnection.disconnect();
		}

		return success;
	}

}
